package com.fuang.enumdemo;

/**
 * Created by Fuang on 2017-7-1.
 * 1，枚举一般是在定义一组固定常量时使用：例如，要定义一组颜色：红色、蓝色、绿色
 * 2，枚举还可以定义一组方法
 */
public enum  EnumDemo {
    MONDAY(PayType.WEEKDAY),
    TUESDAY(PayType.WEEKDAY),
    WEDNESDAY(PayType.WEEKDAY),
    THURSDAY(PayType.WEEKDAY),
    FRIDAY(PayType.WEEKDAY),
    SATURDAY(PayType.WEEKEND),
    SUNDAY(PayType.WEEKEND);

    private PayType payType;
    EnumDemo(PayType payType) {
        this.payType = payType;
    }

    double pay (double hours, double payRate) {
        return payType.overtimePay(hours, payRate);
    }

    /**
     * 用枚举定义一组方法
     */
    private enum PayType{
        WEEKDAY {
            @Override
            double overtimePay(double hours, double payRate) {
                return hours <= HOURS_PER_SHIFT ? 0 : hours - HOURS_PER_SHIFT * payRate / 2;
            }
        },
        WEEKEND {
            @Override
            double overtimePay(double hours, double payRate) {
                return hours * payRate / 2;
            }
        };

        private static final int HOURS_PER_SHIFT = 8;

        abstract double overtimePay(double hours, double payRate);
    }

    public static void main(String[] args) {
        System.out.println(EnumDemo.MONDAY.pay(9, 2));
        System.out.println(EnumDemo.SUNDAY.pay(9, 2));
    }
}
